package jp.co.sharp.android.parents.kidsguard.activity.edit.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.dto.ScheduleDetailDto;
import jp.co.sharp.android.parents.kidsguard.dto.ScheduleDto;

public class ScheduleListAdapter extends ArrayAdapter<ScheduleDto>{

    private int resourceId;
    private OnClickItemListenter onClickItemListenter;
    private Context context;


    public ScheduleListAdapter(@NonNull Context context, int resource, @NonNull List<ScheduleDto> objects, OnClickItemListenter onClickItemListenter) {
        super(context, resource, objects);
        this.context = context;
        this.resourceId = resource;
        this.onClickItemListenter = onClickItemListenter;
    }




    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ScheduleDto scheduleDto = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.month = view.findViewById(R.id.textViewMonth);
            viewHolder.day = view.findViewById(R.id.textViewDay);
            viewHolder.linearLayoutMonth=view.findViewById(R.id.linearLayoutMonth);
            viewHolder.scheduleListItem=view.findViewById(R.id.scheduleListItem);
            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.scheduleListItem.setAdapter(new ScheduleListItemAdapter(context,R.layout.schedule_list_item,scheduleDto.getData(),
                new ScheduleListItemAdapter.OnClickItemListenter(){
                    @Override
                    public void chooseItemClick(ScheduleDetailDto Object, View view) {
                        onClickItemListenter.chooseItemClick(Object, view);
                    }
                }));
        setListViewHeightBasedOnChildren(viewHolder.scheduleListItem);

//        viewHolder.linearLayoutDay.removeAllViews();
//
//        TextView textViewDay1 = view.findViewById(R.id.textViewDay);
//        LinearLayout linearLayout = view.findViewById(R.id.linearLayoutDay);
//
//        for ( ScheduleDetailDto item:scheduleDto.getData()) {
//            TextView textView =new TextView(view.getContext());
//            textView.setText(item.getTitle());
//            textView.setBackgroundResource(R.drawable.text_view_radius);
//            textView.setOnClickListener(new ChooseItemListenter());
//            textView.setWidth(328);
//            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
//            textView.setTextColor(textViewDay1.getTextColors());
//            textView.setPadding(40,40,40,40);
//
//            textView.setHeight(150);
//
//
//            textView.setTag(item.getId());
//            viewHolder.linearLayoutDay.addView(textView);
//        }
        //TODO 这里需要修改 日期格式
        if ("01".equals(scheduleDto.getDate().split("-")[2])){
            viewHolder.linearLayoutMonth.setVisibility(View.VISIBLE);
            viewHolder.month.setText(scheduleDto.getDate());
        }else {
            viewHolder.linearLayoutMonth.setVisibility(View.GONE);
        }

        viewHolder.day.setText(scheduleDto.getDate()+scheduleDto.getDate().split("-")[2]+dateToWeek(scheduleDto.getDate()));
        return view;
    }


    class ViewHolder{
        TextView month;
        TextView day;
        LinearLayout linearLayoutMonth;
        ListView scheduleListItem;
    }

    /**
     *
     */
    public interface OnClickItemListenter {
        void chooseItemClick(ScheduleDetailDto object, View view);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void OnDelBluetoothListenter(OnClickItemListenter callBack) {
        onClickItemListenter = callBack;
    }

    class ChooseItemListenter implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            onClickItemListenter.chooseItemClick((ScheduleDetailDto)view.getTag(), view);
        }
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }


    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "(日)", "(月)", "(火)", "(水)", "(木)", "(金)", "(土)" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }













    }
